package net.tv90.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.servlet.http.HttpServletRequest;
import net.tv90.auth.dao.AccountMapper;
import net.tv90.auth.dao.UserMapper;
import net.tv90.auth.exception.AuthException;
import net.tv90.auth.model.domain.Account;
import net.tv90.auth.model.domain.User;
import net.tv90.auth.model.dto.UserLoginByEmailDTO;
import net.tv90.auth.model.dto.UserLoginByUsernameDTO;
import net.tv90.auth.model.vo.UserInfoVO;
import net.tv90.auth.model.vo.UserLoginVO;
import net.tv90.auth.service.LoginService;
import net.tv90.auth.utils.IpUtils;
import net.tv90.auth.utils.TokenUtils;
import net.tv90.auth.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    AccountMapper accountMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    UserUtils userUtils;

    @Autowired
    TokenUtils tokenUtils;


    @Override
    public UserLoginVO loginByUsername(UserLoginByUsernameDTO dto, HttpServletRequest request) {
        Integer checkIfRegistered = accountMapper.checkUserExistByUserName(dto.getUsername());
        if (checkIfRegistered != null && checkIfRegistered == 0) {
            throw new AuthException("该用户尚未注册", "username");
        }
        Account account = accountMapper.tryLoginUserByUsername(dto.getUsername(), dto.getPassword());
        if (account == null) {
            throw new AuthException("用户名或密码错误", "password");
        }
        if (account.getStatus() == 1) {
            throw new AuthException("该用户已被封禁", "username");
        }
        account.setLoginTimes(account.getLoginTimes() + 1);
        account.setLastLoginIp(IpUtils.getIpAddr(request));
        account.setLastLoginTime(LocalDateTime.now());
        accountMapper.doLogin(account);

        String token = tokenUtils.generateToken(account.getUsername(), account.getPassword());
        userUtils.login(account.getUsername(), token);

        User user = userMapper.selectById(account.getId());

        return new UserLoginVO(account.getUsername(), user.getNickname(), user.getAvatar(), token);
    }

    @Override
    public UserLoginVO loginByEmail(UserLoginByEmailDTO dto, HttpServletRequest request) {
        Integer checkIfRegistered = accountMapper.checkUserExistByEmail(dto.getEmail());
        if (checkIfRegistered != null && checkIfRegistered == 0) {
            throw new AuthException("该邮箱尚未注册", "username");
        }
        Account account = accountMapper.tryLoginUserByEmail(dto.getEmail(), dto.getPassword());
        if (account == null) {
            throw new AuthException("邮箱或密码错误", "password");
        }
        if (account.getStatus() == 1) {
            throw new AuthException("该用户已被封禁", "username");
        }
        account.setLoginTimes(account.getLoginTimes() + 1);
        account.setLastLoginIp(IpUtils.getIpAddr(request));
        account.setLastLoginTime(LocalDateTime.now());
        accountMapper.doLogin(account);

        String token = tokenUtils.generateToken(account.getUsername(), account.getPassword());
        userUtils.login(account.getUsername(), token);

        User user = userMapper.selectById(account.getId());

        return new UserLoginVO(account.getUsername(), user.getNickname(), user.getAvatar(), token);
    }

    @Override
    public void logout(String username) {
        userUtils.logout(username);
    }

    @Override
    public UserInfoVO getUserInfo(String username) {
        QueryWrapper<Account> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        Account account = accountMapper.selectOne(wrapper);
        if(account == null) {
            return null;
        }
        Long id = account.getId();
        User user = userMapper.selectById(id);
        if(user == null) {
            return null;
        }
        return new UserInfoVO(user.getNickname(), user.getAvatar());
    }
}
